package com.oceansoft.mobile.econsole.survey.controller;

import com.alibaba.fastjson.JSONArray;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.survey.entity.Option;
import com.oceansoft.mobile.econsole.survey.entity.Question;
import com.oceansoft.mobile.econsole.survey.entity.QuestionExt;
import com.oceansoft.mobile.econsole.survey.entity.Survey;
import com.oceansoft.mobile.econsole.survey.service.ISurveyService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.DELETE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by liujie on 2016/9/1.
 */
@Controller
@RequestMapping("survey")
public class SurveyController extends BaseController{
    @Resource
    ISurveyService surveyService;

    @RequestMapping(value = "index")
    public String gotoIndex(Query query, Model model) {
        Pager<Survey> page = surveyService.findSurveyByCondition(query);
        model.addAttribute("query", query);
        model.addAttribute("pager", page);
        log("查看调查问卷列表");
        return "survey/survey-list";
    }

    @RequestMapping(value = "view/{id}")
    public String getSurvey(@PathVariable int id, Model model) {
        Survey survey = new Survey();
        if (id != -1) {
            survey = surveyService.getSurveyById(id);
        }
        model.addAttribute("survey", survey);
        return "survey/survey-new";
    }

    /**
     * 更新
     *
     * @param survey
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "edit", method = POST)
    public Result editSurvey(Survey survey) {
        try {
            Result r = surveyService.saveSurvey(survey);
            r.setCallbackType(Result.CLOSECURRENT);
            r.setNavTabId("page_survey_index");
            return r;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILURE, "提交参数有误");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "delete/{id}", method = DELETE)
    public Result deleteSurvey(@PathVariable int id) {
        try {
            Result r = surveyService.deleteSurvey(id);
            return r;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILURE, "删除失败");
        }
    }


    //-----   题目管理开始---------//

    /**
     * 进入题目管理首页（列表）
     *
     * @return String
     */
    @RequestMapping(value = "question/index")
    public String gotoQuestions(Query query, Model model) {
        Pager<Question> page = surveyService.findQuestionByCondition(query);
        List<Survey> surveyList = surveyService.findSurveysByCondition(new Query());
        model.addAttribute("query", query);
        model.addAttribute("pager", page);
        model.addAttribute("surveyList", surveyList);
        log("查看题目列表");
        return "survey/question-list";
    }

    @RequestMapping(value = "question/view/{id}")
    public String getQuestion(@PathVariable int id, Model model) {
        Question question = new Question();
        List<Option> optionlist = new ArrayList<Option>();
        if (id != -1) {
            question = surveyService.getQuestionById(id);
            optionlist = surveyService.getOptionsByQuestion(question);
        }
        List<Survey> surveyList = surveyService.findSurveysByCondition(new Query());
        model.addAttribute("question", question);
        model.addAttribute("surveyList", surveyList);
        model.addAttribute("option", JSONArray.toJSONString(optionlist));
        model.addAttribute("count", optionlist.size());
        return "survey/question-new";
    }

    @ResponseBody
    @RequestMapping(value = "question/edit", method = POST)
    public Result editQuestion(Question question,HttpServletRequest request) {
        try {
            question.setType("1");//单选题
            Result r = surveyService.saveQuestion(question,request);
            r.setCallbackType(Result.CLOSECURRENT);
            r.setNavTabId("page_survey_questions");
            return r;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILURE, "提交参数有误");
        }
    }

    @ResponseBody
    @RequestMapping(value = "question/delete/{id}", method = DELETE)
    public Result deleteQuestion(@PathVariable int id) {
        try {
            Result r = surveyService.deleteQuestion(id);
            return r;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILURE, "删除失败");
        }
    }

}
